package com.smarth.zhizulian.fragment;

import android.os.Bundle;
import android.view.View;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.activity.QRCodeActivity;
import com.smarth.zhizulian.activity.ShareMeActivity;
import com.smarth.zhizulian.app.EventFragment;
import com.smarth.zhizulian.bean.user.DiscipleIndex;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.entity.UserInfo;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.PageObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.CommonUtils;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.ShareUtils;
import com.smarth.zhizulian.utils.UserUtils;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.PageDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.model.PageDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;
import com.smarth.zhizulian.view.LoadingLayout;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by 18865 on 2017/12/21.
 */

public class InviteDiscipleFragment extends EventFragment {
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    private DiscipleIndex mDiscipleIndex;
    private RefreshHelper<DiscipleIndex> refreshHelper;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.fragment_inviteapprentice);
        init();
    }

    private void init() {
        if (UserUtils.isLogin()) {
            refreshHelper = new RefreshHelper<>(mContext, llLoadinglayout, null);
            refreshHelper.setDataModel(new PageDataModel<DiscipleIndex>() {
                @Override
                public void fetch(ResponseSender<DiscipleIndex> responseSender) {
                    KumaHttp.getService().discipleIndex()
                            .compose(RxCompos.<DiscipleIndex>getBELoginCompos())
                            .subscribe(new PageObserver<DiscipleIndex>(mContext, responseSender));
                }
            });
            refreshHelper.setDataAdapter(new PageDataAdapter<DiscipleIndex>() {
                @Override
                public void onGetData(DiscipleIndex discipleIndex, boolean isRefresh) {
                    super.onGetData(discipleIndex, isRefresh);
                    mDiscipleIndex = discipleIndex;
                }
            });
            refreshHelper.refresh();
        }
    }

    @Override
    protected void onLogin(EventBean eb) {
//        fetchUserShare();
//        refreshHelper.loadMore();
//        refreshHelper.refresh();
        init();
    }

    @Subscriber(tag = "InviteDiscipleFragment")
    private void onEvent(EventBean eb) {
        if (mDiscipleIndex == null) {
            KumaToast.show(mContext, getString(R.string.network_access));
        } else {
//            shareWechat();
            ShareUtils.shareForAddFriends(mContext, mDiscipleIndex, Wechat.NAME);
        }
    }

//    private void shareWechat() {
//        Platform.ShareParams sp = new Platform.ShareParams();
//        sp.setTitle("我送您一个现金红包，点击即可领取哦~");
//        sp.setText("祝您新春快乐，阖家幸福安康！");
////        sp.setImageUrl(BaseConfig.NetWork.LOGO);
//        sp.setImageData(BitmapFactory.decodeResource(getResources(), R.drawable.share_wechathongbao));
//        sp.setUrl(mDiscipleIndex.getWxShareUrl());
//        sp.setShareType(Platform.SHARE_WEBPAGE);
//        Platform wechat = ShareSDK.getPlatform(cn.sharesdk.wechat.friends.Wechat.NAME);
//        wechat.setPlatformActionListener(new PlatformActionListener() {
//            @Override
//            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//                KumaToast.show(mContext, "分享完成");
//            }
//
//            @Override
//            public void onError(Platform platform, int i, Throwable throwable) {
//                KumaToast.show(mContext, "分享失败");
//            }
//
//            @Override
//            public void onCancel(Platform platform, int i) {
//                KumaToast.show(mContext, "分享取消");
//            }
//        });
//        wechat.share(sp);
//    }
//
//    private void sharePengyou() {
//        KumaHttp.getService().downloadFile(mDiscipleIndex.getQrCodeImageUrl())
//                .compose(RxCompos.<ResponseBody>getHttpCompos())
//                .subscribe(new ProgressObserver<ResponseBody>(mContext, true) {
//                    @Override
//                    public void onNext(ResponseBody responseBody) {
//                        Bitmap qrCodeBitmap = BitmapFactory.decodeStream(responseBody.byteStream());
//                        if (qrCodeBitmap != null) {
//                            final Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pengyouquan2).copy(Bitmap.Config.ARGB_8888, true);
//                            Canvas canvas = new Canvas(mBitmap);
//                            int width = mBitmap.getWidth();
//                            int height = mBitmap.getHeight();
//
////                            int qrSize = (int) (0.3453f * width * (1 - 0.0386 * 2));
//                            int qrSize = (int) (0.3333f * width);
//                            qrCodeBitmap = Bitmap.createScaledBitmap(qrCodeBitmap, qrSize, qrSize, false);
//                            Rect qrRect = new Rect();
////                            qrRect.left = (int) (width * 0.328f + (width * 0.3453f * 0.0386f));
////                            qrRect.top = (int) (height * 0.6769f + (width * 0.3453f * 0.0386f));
//                            qrRect.left = (int) (width * 0.3346f);
//                            qrRect.top = (int) (height * 0.6814f);
//                            qrRect.right = qrRect.left + qrSize;
//                            qrRect.bottom = qrRect.top + qrSize;
//
//                            canvas.drawBitmap(qrCodeBitmap, null, qrRect, null);
//
//                            Platform.ShareParams sp = new Platform.ShareParams();
//                            sp.setText("【健康头条】分享就能赚钱，每天签到赚钱倍增，天天都有幸运红包一元就可提现，收徒更有30%奖励提成，现在下载注册最高可送66元现金，亲测绝对真实哦");
//                            sp.setImageData(mBitmap);
//                            sp.setShareType(Platform.SHARE_IMAGE);
//                            Platform moment = ShareSDK.getPlatform(WechatMoments.NAME);
//                            moment.setPlatformActionListener(new PlatformActionListener() {
//                                @Override
//                                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//                                    KumaToast.show(mContext, "分享完成");
//                                }
//
//                                @Override
//                                public void onError(Platform platform, int i, Throwable throwable) {
//                                    KumaToast.show(mContext, "分享失败");
//                                }
//
//                                @Override
//                                public void onCancel(Platform platform, int i) {
//                                    KumaToast.show(mContext, "分享取消");
//                                }
//                            });
//                            moment.share(sp);
//
//                        } else {
//                            KumaToast.show(mContext, "分享失败");
//                        }
//                    }
//                });
//
//
//    }

    @OnClick({R.id.iv_pengyou, R.id.iv_wechat, R.id.iv_copy, R.id.iv_qrcode, R.id.iv_shareme, R.id.iv_qa, R.id.iv_qa2, R.id.iv_message, R.id.iv_qq})
    public void onViewClicked(View view) {
        if (!UserUtils.isLogin()) {
            LoginActivity.open(mContext);
            return;
        }
        switch (view.getId()) {
            case R.id.iv_pengyou:
//                sharePengyou();
                ShareUtils.shareForAddFriends(mContext, mDiscipleIndex, WechatMoments.NAME);
                break;
            case R.id.iv_wechat:
//                shareWechat();
                ShareUtils.shareForAddFriends(mContext, mDiscipleIndex, Wechat.NAME);
                break;
            case R.id.iv_qq:
                ShareUtils.shareForAddFriends(mContext, mDiscipleIndex, QQ.NAME);
                break;
            case R.id.iv_copy:
                CommonUtils.copyToClipboard(mContext, "【直租链】分享就能赚钱，每天签到赚钱倍增，天天都有幸运红包，收徒更有30%奖励提成，现在下载注册最高可送66元现金，亲测绝对真实哦！下载链接:" + mDiscipleIndex.getWxShareUrl());
                break;
            case R.id.iv_qrcode:
//                KumaToast.show(mContext, "等设计图");
                QRCodeActivity.open(mContext, mDiscipleIndex.getQrCodeImageUrl());
                break;
            case R.id.iv_shareme:
                UserInfo ui = new UserInfo();
                ui.setAvatar(mDiscipleIndex.getAcatar());
                ui.setCoin(mDiscipleIndex.getCoin());
                ui.setName(mDiscipleIndex.getNickname());
                ui.setQrcode(mDiscipleIndex.getQrCodeImageUrl());
                ui.setWechatUrl(mDiscipleIndex.getWxShareUrl());
                ShareMeActivity.open(mContext, ui);
                break;
            case R.id.iv_qa:
            case R.id.iv_qa2:
                AgentWebActivity.openForQuestion(mContext);
                break;
            case R.id.iv_message:
                CommonUtils.sendSMS(mContext, "亲，推荐你一款好用的软件【直租链】，立即注册就可以获得3～66元奖励，每天千万级红包雨下不停，真金白银一元就可提现！领取福利请点击：" + mDiscipleIndex.getWxShareUrl() + " 添加微信：BZ1226372200了解更多");
                break;
        }
    }

}
